package net.tuandn.training.lesson.gson;

import java.lang.reflect.Type;
import java.util.List;

import net.tuandn.training.model.City;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class CollectionDeserializer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*
		 * list of cities
		 * 
		 * List<City> cities = new ArrayList<City>(); cities.add(new City(1,
		 * "Hanoi", 4000)); cities.add(new City(2, "Haiphong", 2000));
		 * cities.add(new City(3, "Hue", 1500)); cities.add(new City(4,
		 * "Saigon", 6000));
		 */
		String citiesListJson = "[{\"id\":1,\"name\":\"Hanoi\",\"population\":4000}," + "{\"id\":2,\"name\":\"Haiphong\",\"population\":2000}," + "{\"id\":3,\"name\":\"Hue\",\"population\":1500}," + "{\"id\":4,\"name\":\"Saigon\",\"population\":6000}]";

		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		Type citiesListType = new TypeToken<List<City>>() {
		}.getType();
		List<City> myCities = gson.fromJson(citiesListJson, citiesListType);

		for (int i = 0; i < myCities.size(); i++) {
			System.out.println(gson.toJson(myCities.get(i)));
		}
	}

}
